 #ifndef __TCP_LINK_H__
#define __TCP_LINK_H__

#include "tlv.h"
#include "socket_class.hpp"
#include "network.hpp"
#include "io.hpp"
#include <string>
#include <memory>
#include <functional>

using std::shared_ptr;
using std::string;
using std::function;

namespace rocket 
{
class epoll_loop;    
class tcp_link
:public std::enable_shared_from_this<tcp_link>
{
public:
    tcp_link(int,epoll_loop *) ;
    ~tcp_link() ;
    int receive(string &,int);
    int read_packet(packet &);
    int send(const string,int);
    void new_link();
    void old_link();
    void close_link();
    void set_callback(function<void(shared_ptr<tcp_link>)>callback1,
            function<void(shared_ptr<tcp_link>)>callback2,
            function<void(shared_ptr<tcp_link>)>callback3);
    string get_link_line();
    void shutdown_write();
    void add_send_task(string data);
    void add_send_task(tlv data);
    bool link_status();
    string get_user();
private:
    int peek(string &,int);
    sockaddr_in local_address();
    sockaddr_in peer_address();

private:
    socket_class _client_fd;
    network _local_address;
    network _peer_address;
    io _io;
    epoll_loop *_loop;
    string _user;
    function<void(shared_ptr<tcp_link>)> _new;
    function<void(shared_ptr<tcp_link>)> _old;
    function<void(shared_ptr<tcp_link>)> _close;

};

}

#endif